package com.liull.netty.itcast.nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;

/**
 * @Author: liull
 * @Description: nio之Selector学习
 * @Date: Created in 0:48 2020/10/20
 * @Modified By:
 */
public class NioTest1 {
    public static void main(String[] args) throws IOException {
        int[] ports = new int[5];
        ports[0] = 5000;
        ports[1] = 5001;
        ports[2] = 5002;
        ports[3] = 5003;
        ports[4] = 5004;
        Selector selector = Selector.open();
        for (int i = 0; i < ports.length; i++) {
            ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
            serverSocketChannel.configureBlocking(false);
            // 获取该通道相关联的ServerSocket
            ServerSocket serverSocket = serverSocketChannel.socket();
            // 绑定serverSocket至给定的地址
            serverSocket.bind(new InetSocketAddress("127.0.0.1", ports[i]));
            // 使用给定的选择器注册此通道，并返回SelectionKey
            serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
            System.out.println("正在监听端口：" + ports[i]);
        }
        while (true) {
            int numbers = selector.select();
            System.out.println("selector.select() = " + numbers);
            Set<SelectionKey> selectionKeys = selector.selectedKeys();
            Iterator<SelectionKey> iterator = selectionKeys.iterator();
            while (iterator.hasNext()) {
                SelectionKey selectionKey = iterator.next();
                if (selectionKey.isAcceptable()) { // 接收连接
                    ServerSocketChannel serverSocketChannel1 = (ServerSocketChannel) selectionKey.channel();
                    SocketChannel socketChannel = serverSocketChannel1.accept();
                    socketChannel.configureBlocking(false);
                    socketChannel.register(selector, SelectionKey.OP_READ);
                    iterator.remove();// 极其重要
                    System.out.println("获取到客户端连接 " + socketChannel);
                } else if (selectionKey.isReadable()) {
                    SocketChannel socketChannel = (SocketChannel) selectionKey.channel();
                    int bytesRead = 0;
                    while (true) {
                        ByteBuffer buffer = ByteBuffer.allocate(1024);
                        buffer.clear();
                        int read = socketChannel.read(buffer);
                        if (read <= 0) {
                            break;
                        }
                        buffer.flip();
                        socketChannel.write(buffer);
                        bytesRead += read;
                    }
                    System.out.println("读取：" + bytesRead + "，来自于" + socketChannel);
                    iterator.remove();

                } else if (selectionKey.isWritable()) {

                } else {
                    System.out.println("读写，accept之外" + selectionKey);
                }
            }

        }
    }
}
